/*
 *  $TEAM_NAME Space Trader
 */
package views;
import models.StarSystem;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.AlphaComposite;
import java.awt.event.ActionListener;

import javax.swing.JButton;

/**
 * Is the view the user sees if he clicks on a system to see its properties in map view
 * @author bradley
 * @version 1.0
 */
public class MapOverlayPanel extends JPanel { 
	
	/**
	 * Field TWENTY.
	 * (value is 20)
	 */
	private static final int TWENTY = 20;

	/**
	 * Field SEVENFIVE.
	 * (value is 0.75)
	 */
	private static final float SEVENFIVE = 0.75f;

	/**
	 * Field FOUR.
	 * (value is 4)
	 */
	private static final int FOUR = 4;

	/**
	 * Field FIFTEEN.
	 * (value is 15)
	 */
	private static final int FIFTEEN = 15;

	/**
	 * Field THIRTY.
	 * (value is 30)
	 */
	private static final int THIRTY = 30;

	/**
	 * Field FORTYFIVE.
	 * (value is 45)
	 */
	private static final int FORTYFIVE = 45;
	
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * field
	 */
	private final StarSystem system;

	/**
	 * field
	 */
	private JButton warpButton = null;

    /**
     * Default constructor
     * @param system - the system to show details for
     */
    public MapOverlayPanel(StarSystem system) {
    	//Set local variables
        this.system = system;

        //Set layout properties
        super.setLayout(null);

        //Add the warp button
        if(system.getInRange()){
	        warpButton = new JButton("Warp!");
	        super.add(warpButton);
        }
    }

    /**
     * Set the bounds of the overlay panel
     * @param x int
     * @param y int
     * @param width int
     * @param height int
     */
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        
        //set the warp button bounds
        if(this.system.getInRange()){
        	if (warpButton != null){
        		warpButton.setBounds(0, height - TWENTY, width, TWENTY);
        	}
        }
    }

    /**
     * Paints the info overlay on top of the map
     * @param g Graphics
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        final Graphics2D gfx = (Graphics2D) g;
        final Dimension size = getSize();

        gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, SEVENFIVE));

        gfx.setColor(Color.BLACK);
        gfx.fillRect(0, 0, size.width, size.height);

        gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, SEVENFIVE));
        gfx.setColor(Color.WHITE);

        gfx.drawRect(0, 0, size.width - 1, size.height - 1);

        gfx.drawString(system.getName(), FOUR, FIFTEEN);
        gfx.drawString(system.getTechLevel().toString(), FOUR, THIRTY);
        gfx.drawString(system.getResourceCondition().toString(), FOUR, FORTYFIVE);
    }

    /**
     * Adds an event listener to the overlay panel
     * @param al - Action listener
     */
    public void addActionListener(ActionListener al) {
    	if(this.system.getInRange()){
    		warpButton.addActionListener(al);
    	}
    }

    /**
     * Getter for the star system
    
     * @return - gets the star system */
    public StarSystem getStarSystem() {
        return system;
    }
}
